package com.ruoyi.wechat.utils;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.wechat.common.Constants;
import com.ruoyi.wechat.common.ServiceException;
import com.ruoyi.wechat.domain.AppUser;
import com.ruoyi.wechat.domain.SessionInfo;
import org.apache.http.HttpStatus;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 登录助手
 * </p>
 *
 * @author YinHeng
 * @since 2024/2/25
 */
public class LoginHelper {

    private static ThreadLocal<String> tokenThreadLocal = new ThreadLocal<>();

    /**
     * 获取登录用户
     *
     * @return
     */
    public static SessionInfo getSession() {
        String key = getKey();
        String value = RedisHelper.get(key);
        if (ObjectUtils.isEmpty(value)) {
            throw new ServiceException(HttpStatus.SC_UNAUTHORIZED, "登录已过期，请从新登录");
        }
        SessionInfo sessionInfo = JSON.parseObject(value, SessionInfo.class);

        if (ObjectUtils.isEmpty(sessionInfo.getAppUser())) {
            throw new ServiceException(HttpStatus.SC_UNAUTHORIZED, "登录已过期，请从新登录");
        }
        return sessionInfo;
    }

    /**
     * 获取登录redisKey
     *
     * @return
     */
    public static String getKey() {
        String token = tokenThreadLocal.get();
        if (ObjectUtils.isEmpty(token)) {
            throw new ServiceException(HttpStatus.SC_UNAUTHORIZED, "未登录");
        }
        String key = Constants.Login.TOKEN_KEY_PRE + token;
        return key;
    }

    /**
     * 刷新登录会话信息
     *
     * @param sessionInfo
     * @return
     */
    public static void refreshSession(SessionInfo sessionInfo) {
        String key = getKey();
        RedisHelper.setEx(key, 10L, JSON.toJSONString(sessionInfo));
    }

    /**
     * 刷新登录会话信息
     *
     * @param appUser
     */
    public static void refreshSession(AppUser appUser) {
        String key = getKey();
        SessionInfo session = getSession();
        session.setAppUser(appUser);
        RedisHelper.setEx(key, 600L, JSON.toJSONString(session));
    }

    /**
     * 检查授权
     */
    public static void checkAuth() {
        AppUser appUser = getSession().getAppUser();
        if (ObjectUtils.isEmpty(appUser.getPhone())) {
            throw new ServiceException(HttpStatus.SC_PRECONDITION_FAILED, "未授权手机号");
        }
    }


    /**
     * 获取当前用户sessionKey
     *
     * @return
     */
    public static String getSessionKey() {
        SessionInfo sessionInfo = getSession();
        String sessionKey = sessionInfo.getSessionKey();
        if (ObjectUtils.isEmpty(sessionKey)) {
            throw new ServiceException(HttpStatus.SC_UNAUTHORIZED, "登录用户会话密钥为空");
        }
        return sessionKey;

    }

    /**
     * 设置token到当前线程
     *
     * @param token
     */
    public static void setToken(String token) {
        tokenThreadLocal.set(token);
    }

    /**
     * 获取登录口令
     */
    public static String getToken() {
        String token = tokenThreadLocal.get();
        return token;
    }

    /**
     * 移除当前线程缓存
     */
    public static void removeToken() {
        tokenThreadLocal.remove();
    }

}
